import { createRouter, createWebHistory } from 'vue-router'
import login from "@/views/user/login/index.vue"
import permission from './permission'
import {authRouteMode} from "@/common/config"
import sysLayout_routes from "./route.config"

// 系统布局外的路由肯定不多，直接写这就行了
const constRoutes=[
  {
    path: '/login',
    name: 'login',
    meta:{title:'登录'},
    component:login          // 初始时需要加载，就不采用懒加载了 
  },
  { 
    // router4不支持*匹配，现在修改为这样了。这个路由会匹配不在路由配置中的一切路由
    path:'/:pathMatch(.*)*',    
    meta:{title:'404'},
    name:'404',
    component: () => import('@/views/404.vue'),
  },
 
]

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHistory(),
  routes:authRouteMode=='local'?[...constRoutes,...sysLayout_routes] as any[]:constRoutes,
})

// 全局路由守卫
permission(router)

// 重置路由
export function resetRouter() {
  router.getRoutes().forEach(route => {
    const {name} = route
    if (name) router.removeRoute(name)
  })
  constRoutes.forEach(v=>{router.addRoute(v)})
}


export default router
